package com.qk.service.impl;

import com.qk.mapper.BusinessMapper;
import com.qk.mapper.ClueMapper;
import com.qk.service.ReportService;
import com.qk.vo.OverviewVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private BusinessMapper businessMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public OverviewVo getOverview() {
        Object overview = redisTemplate.opsForValue().get("overview");
        log.info("缓存值为overview:{}", overview);
        if (overview != null){
            return (OverviewVo) overview;
        }

        //获取线索表总览
        OverviewVo clueOverviewVo = clueMapper.getOverview();
        //获取商机表总览
        OverviewVo businessOverviewVo = businessMapper.getOverview();
        //合并
        BeanUtils.copyProperties(clueOverviewVo, businessOverviewVo,
                "businessTotal","businessWaitAllot", "businessWaitFollow",
                "businessFollowing", "businessFalse", "businessConvertCustomer");

        log.info("进行缓存");
        redisTemplate.opsForValue().set("overview",businessOverviewVo,10, TimeUnit.MINUTES);

        return businessOverviewVo;
    }
}
